package com.amigo.online.provider;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import com.amigo.online.provider.sync.util.VisiableThreadPoolTaskExecutor;
import com.google.common.util.concurrent.ThreadFactoryBuilder;

/**
 * @version: V 1.0.0
 * @Description: （线程池配置中心）
 * @author: zxy  QQ:1363320658
 * @date: 2018年12月10日 下午3:05:08
 */
@Configuration
public class ThreadPoolConfig {

    /**
     * @throws
     * @Title: asyncServiceExecutor
     * @Description: (异步执行)
     * @param: @return
     * @return: Executor
     */
    @Bean
    @Primary
    public Executor asyncServiceExecutor() {
        ThreadPoolTaskExecutor executor = new VisiableThreadPoolTaskExecutor();
        // 配置核心线程数
        executor.setCorePoolSize(16);
        // 配置最大线程数
        executor.setMaxPoolSize(64);
        // 配置队列大小
        executor.setQueueCapacity(99999);
        // 配置线程池中的线程的名称前缀
        executor.setThreadNamePrefix("async-service-");

        // rejection-policy：当pool已经达到max size的时候，如何处理新任务
        // CALLER_RUNS：不在新线程中执行任务，而是有调用者所在的线程来执行
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        // 执行初始化
        executor.initialize();
        return executor;
    }

    /**
     * @throws
     * @Title: buildConsumerQueueThreadPool
     * @Description: ()
     * @param: @return
     * @return: ExecutorService
     */
    @Bean(value = "videoQueueThreadPool")
    public ExecutorService buildVideoQueueThreadPool() {
        ThreadFactory namedThreadFactory = new ThreadFactoryBuilder()
                .setNameFormat("video-queue-thread-%d").build();
        ExecutorService pool = new ThreadPoolExecutor(4, 1024, 0L, TimeUnit.MILLISECONDS,
                new ArrayBlockingQueue<Runnable>(2048), namedThreadFactory, new ThreadPoolExecutor.AbortPolicy());
        return pool;
    }

    @Bean(value = "goodsQueueThreadPool")
    public ExecutorService buildGoodsQueueThreadPool() {
        ThreadFactory namedThreadFactory = new ThreadFactoryBuilder()
                .setNameFormat("goods-queue-thread-%d").build();
        ExecutorService pool = new ThreadPoolExecutor(4, 1024, 0L, TimeUnit.MILLISECONDS,
                new ArrayBlockingQueue<Runnable>(2048), namedThreadFactory, new ThreadPoolExecutor.AbortPolicy());
        return pool;
    }

    @Bean(value = "userQueueThreadPool")
    public ExecutorService buildUserQueueThreadPool() {
        ThreadFactory namedThreadFactory = new ThreadFactoryBuilder()
                .setNameFormat("user-queue-thread-%d").build();
        ExecutorService pool = new ThreadPoolExecutor(4, 1024, 0L, TimeUnit.MILLISECONDS,
                new ArrayBlockingQueue<Runnable>(2048), namedThreadFactory, new ThreadPoolExecutor.AbortPolicy());
        return pool;
    }

    @Bean(value = "musicQueueThreadPool")
    public ExecutorService buildMusicQueueThreadPool() {
        ThreadFactory namedThreadFactory = new ThreadFactoryBuilder()
                .setNameFormat("music-queue-thread-%d").build();
        ExecutorService pool = new ThreadPoolExecutor(4, 1024, 0L, TimeUnit.MILLISECONDS,
                new ArrayBlockingQueue<Runnable>(2048), namedThreadFactory, new ThreadPoolExecutor.AbortPolicy());
        return pool;
    }

    @Bean(value = "merchantQueueThreadPool")
    public ExecutorService buildMerchantQueueThreadPool() {
        ThreadFactory namedThreadFactory = new ThreadFactoryBuilder()
                .setNameFormat("merchant-queue-thread-%d").build();
        ExecutorService pool = new ThreadPoolExecutor(4, 1024, 0L, TimeUnit.MILLISECONDS,
                new ArrayBlockingQueue<Runnable>(2048), namedThreadFactory, new ThreadPoolExecutor.AbortPolicy());
        return pool;
    }

    /**
     * @throws
     * @Title: buildupdateQueueThreadPool
     * @Description: (这里用一句话描述这个方法的作用)
     * @param: @return
     * @return: ExecutorService
     */
    @Bean(value = "updateQueueThreadPool")
    public ExecutorService buildupdateQueueThreadPool() {
        ThreadFactory namedThreadFactory = new ThreadFactoryBuilder()
                .setNameFormat("update-queue-thread-%d").build();
        ExecutorService pool = new ThreadPoolExecutor(16, 64, 6L, TimeUnit.MILLISECONDS,
                new ArrayBlockingQueue<Runnable>(10240), namedThreadFactory, new ThreadPoolExecutor.AbortPolicy());

        return pool;
    }

    /**
     * @throws
     * @Title: buildInsertQueueThreadPool
     * @Description: (这里用一句话描述这个方法的作用)
     * @param: @return
     * @return: ExecutorService
     */
    @Bean(value = "insertQueueThreadPool")
    public ExecutorService buildInsertQueueThreadPool() {
        ThreadFactory namedThreadFactory = new ThreadFactoryBuilder()
                .setNameFormat("insert-queue-thread-%d").build();
        ExecutorService pool = new ThreadPoolExecutor(16, 64, 6L, TimeUnit.MILLISECONDS,
                new ArrayBlockingQueue<Runnable>(10240), namedThreadFactory, new ThreadPoolExecutor.AbortPolicy());

        return pool;
    }

    /**
     * @throws
     * @Title: buildComparsionQueueThreadPool
     * @Description: (这里用一句话描述这个方法的作用)
     * @param: @return
     * @return: ExecutorService
     */
    @Bean(value = "comparsionQueueThreadPool")
    public ExecutorService buildComparsionQueueThreadPool() {
        ThreadFactory namedThreadFactory = new ThreadFactoryBuilder()
                .setNameFormat("consumer-queue-thread-%d").build();
        ExecutorService pool = new ThreadPoolExecutor(5, 5, 0L, TimeUnit.MILLISECONDS,
                new ArrayBlockingQueue<Runnable>(5), namedThreadFactory, new ThreadPoolExecutor.AbortPolicy());

        return pool;
    }

    /**
     * @throws
     * @Title: buildUpdateThreadPool
     * @Description: (这里用一句话描述这个方法的作用)
     * @param: @return
     * @return: ExecutorService
     */
    @Bean(value = "QueueThreadPool")
    public ExecutorService buildUpdateThreadPool() {
        ThreadFactory namedThreadFactory = new ThreadFactoryBuilder()
                .setNameFormat("consumer-queue-thread-%d").build();
        ExecutorService pool = new ThreadPoolExecutor(12, 64, 0L, TimeUnit.MILLISECONDS,
                new ArrayBlockingQueue<Runnable>(5), namedThreadFactory, new ThreadPoolExecutor.AbortPolicy());

        return pool;
    }
}